package com.powerhua.workflow.utils;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.db.GraphSession;
import org.jbpm.graph.def.ProcessDefinition;

public class UndeployWorkflowDefinition implements IUndeployWorkflowDefinition {

	private Log log = LogFactory.getLog(this.getClass());
	private static IUndeployWorkflowDefinition instance;

	public static IUndeployWorkflowDefinition getInstance() {
		if (UndeployWorkflowDefinition.instance == null) {
			UndeployWorkflowDefinition.instance = new UndeployWorkflowDefinition();
		}
		return UndeployWorkflowDefinition.instance;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.withub.common.util.IUninstallProcessDefinition#uninstallJbpmProcessDefinitionsTransaction(java.lang.String)
	 */
	public void uninstallJbpmProcessDefinitionsTransaction(String name) {
		JbpmContext jbpmContext = JbpmConfiguration.getInstance()
				.createJbpmContext();
		try {

			this.uninstallJbpmProcessDefinitionsDao(name);

		} finally {
			jbpmContext.close();

		}

	}

	/**
	 * 
	 * @param name
	 */
	private void uninstallJbpmProcessDefinitionsDao(String name) {
		JbpmContext jbpmContext = JbpmConfiguration.getInstance()
				.getCurrentJbpmContext();
		GraphSession graphSession = jbpmContext.getGraphSession();
		List processDefinitions = graphSession
				.findAllProcessDefinitionVersions(name);
		if (processDefinitions != null) {
			Iterator iterator = processDefinitions.iterator();
			while (iterator.hasNext()) {

				graphSession
						.deleteProcessDefinition((ProcessDefinition) iterator
								.next());
			}

		}
		log.info("卸载工作流：" + name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.withub.common.util.IUninstallProcessDefinition#uninstallJbpmProcessDefinitionTransaction(java.lang.String,
	 *      int)
	 */
	public void uninstallJbpmProcessDefinitionTransaction(String name,
			int version) {
		JbpmContext jbpmContext = JbpmConfiguration.getInstance()
				.createJbpmContext();
		try {

			this.uninstallJbpmProcessDefinitionDao(name, version);

		} finally {
			jbpmContext.close();

		}

	}

	private void uninstallJbpmProcessDefinitionDao(String name, int version) {
		JbpmContext jbpmContext = JbpmConfiguration.getInstance()
				.getCurrentJbpmContext();
		GraphSession graphSession = jbpmContext.getGraphSession();
		graphSession.deleteProcessDefinition(graphSession
				.findProcessDefinition(name, version));
		log.info("卸载工作流：" + name + "第" + new Integer(version) + "版");

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.withub.common.util.IUninstallProcessDefinition#uninstallLatestJbpmProcessDefinitionTransaction(java.lang.String)
	 */
	public void uninstallLatestJbpmProcessDefinitionTransaction(String name) {
		JbpmContext jbpmContext = JbpmConfiguration.getInstance()
				.createJbpmContext();
		try {

			this.uninstallLatestJbpmProcessDefinitionDao(name);

		} finally {
			jbpmContext.close();

		}

	}

	/**
	 * 
	 * @param name
	 */
	private void uninstallLatestJbpmProcessDefinitionDao(String name) {
		JbpmContext jbpmContext = JbpmConfiguration.getInstance()
				.getCurrentJbpmContext();
		GraphSession graphSession = jbpmContext.getGraphSession();
		ProcessDefinition processDefinition = graphSession
				.findLatestProcessDefinition(name);
		graphSession.deleteProcessDefinition(processDefinition);
		log.info("卸载工作流最新版：" + name);
	}

	/**
	 * @param args
	 *            测试
	 */
	public static void main(String[] args) {
		/**
		 * 1,一个参数，全部 2，二个参数 名字，latest 最新版本 版本号
		 */
		IUndeployWorkflowDefinition uninstallJbpmProcessDefinition = UndeployWorkflowDefinition
				.getInstance();

		if (args.length == 1) {
			uninstallJbpmProcessDefinition
					.uninstallJbpmProcessDefinitionsTransaction(args[0]);
		} else if (args.length == 2) {
			if (args[1].equalsIgnoreCase("latest")) {
				uninstallJbpmProcessDefinition
						.uninstallLatestJbpmProcessDefinitionTransaction(args[0]);

			} else {

				uninstallJbpmProcessDefinition
						.uninstallJbpmProcessDefinitionTransaction(args[0],
								Integer.parseInt(args[1]));
			}

		} else {
			throw new RuntimeException(
					"参数数目不正确！ 参数：1，一个参数，提供要卸载的工作流的名称； 2，两个参数，第一个是要卸载的最新版本的工作流名称,第二个参数是latest； 3,两个参数，"
							+ "第一个是要卸载的工作流名称，第二个参数是要修改的工作流的版本号。");

		}

	}
}
